package com.crealth.scheduler;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

import com.crealth.job.CreateCdrTableJob;
import com.crealth.job.CreateVisitTableJob;
import com.crealth.job.HeartbeatJob;
import com.crealth.job.KeepAliveMySQLJob;
import com.crealth.job.MonthFirstDayJob;
import com.crealth.job.StaffDayKPIJob;
import com.crealth.job.TodayEndJob;
import com.crealth.utils.Utils;

public class QuartzScheduler {

	public static void startScheduler()throws Exception {
		String osName = Utils.getOSName();
		if (osName.startsWith("Window")) {
			return;
		}
		
		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler sched = sf.getScheduler(); 
		
		JobDetail job = JobBuilder.newJob(CreateCdrTableJob.class)
				.withIdentity("dayJob", "dayGroup").build(); 
		CronTrigger trigger = TriggerBuilder.newTrigger()
				.withIdentity("trigger1", "dayGroup")
				.withSchedule(CronScheduleBuilder.cronSchedule("5 0 0 * * ?")).build(); 
		sched.scheduleJob(job, trigger);
		
		JobDetail job2 = JobBuilder.newJob(CreateVisitTableJob.class)
				.withIdentity("monthJob", "monthGroup").build(); 
		CronTrigger trigger2 = TriggerBuilder.newTrigger().
				withIdentity("trigger2", "monthGroup")
				.withSchedule(CronScheduleBuilder.cronSchedule("5 0 0 1 * ?")).build(); 
		sched.scheduleJob(job2, trigger2);
		
		//  心跳  检查是否下线
		JobDetail job3 = JobBuilder.newJob(HeartbeatJob.class)
				.withIdentity("heartbeatJob", "heartbeatGroup").build(); 
		CronTrigger trigger3 = TriggerBuilder.newTrigger().
				withIdentity("trigger3", "heartbeatGroup")
				.withSchedule(CronScheduleBuilder.cronSchedule("0 0/1 * * * ?")).build(); 
		sched.scheduleJob(job3, trigger3);
		
		//  保持 MySQL 的连接
		JobDetail job4 = JobBuilder.newJob(KeepAliveMySQLJob.class)
				.withIdentity("KeepAliveMysql", "KeepAliveGroup").build(); 
		CronTrigger trigger4 = TriggerBuilder.newTrigger().
				withIdentity("trigger4", "KeepAliveGroup")
				.withSchedule(CronScheduleBuilder.cronSchedule("0 0 0/1 * * ?")).build(); 
		sched.scheduleJob(job4, trigger4);
		
		//  每天电话统计 KPI
		JobDetail job5 = JobBuilder.newJob(StaffDayKPIJob.class)
				.withIdentity("StaffDayKPI", "StaffDayKPIGroup").build(); 
		CronTrigger trigger5 = TriggerBuilder.newTrigger().
				withIdentity("trigger5", "StaffDayKPIGroup")
				.withSchedule(CronScheduleBuilder.cronSchedule("0 0 23 * * ?")).build(); 
		sched.scheduleJob(job5, trigger5);
		
		// 每个月 1号  MOB++
		JobDetail job6 = JobBuilder.newJob(MonthFirstDayJob.class)
				.withIdentity("MonthFirstDay", "MonthFirstDayGroup").build(); 
		CronTrigger trigger6 = TriggerBuilder.newTrigger().
				withIdentity("trigger6", "MonthFirstDayGroup")
				.withSchedule(CronScheduleBuilder.cronSchedule("0 0 */1 * * ?")).build(); 
		sched.scheduleJob(job6, trigger6);
		
		//  每天结束   处理一些数据   晚上10点
		JobDetail job7 = JobBuilder.newJob(TodayEndJob.class)
				.withIdentity("TodayEnd", "TodayEndGroup").build(); 
		CronTrigger trigger7 = TriggerBuilder.newTrigger().
				withIdentity("trigger7", "TodayEndGroup")
				.withSchedule(CronScheduleBuilder.cronSchedule("0 0 22 * * ?")).build(); 
		sched.scheduleJob(job7, trigger7);
		
		sched.start();
		
		//sched.shutdown();
	}
	
}
